<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="巡检班组" prop="team">
                <el-input
                        v-model="queryParams.team"
                        placeholder="请输入巡检班组"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="巡检区域" prop="region">
                <el-input
                        v-model="queryParams.region"
                        placeholder="请输入巡检区域"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable
                                v-model="queryParams.startTime"
                                type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss"
                                placeholder="请选择计划开始时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable
                                v-model="queryParams.endTime"
                                type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss"
                                placeholder="请选择计划结束时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary"
                           plain
                           icon="Plus"
                           @click="handleAdd"
                           v-hasPermi="['ar_check:lanRecord:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['ar_check:lanRecord:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="lanRecordList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="巡检计划" align="center" prop="planId"/>
            <el-table-column label="巡检班组" align="center" prop="team"/>
            <el-table-column label="巡检区域" align="center" prop="region"/>
            <el-table-column label="计划开始时间" align="center" prop="startTime" width="180">
            </el-table-column>
            <el-table-column label="计划结束时间" align="center" prop="endTime" width="180"/>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改巡检计划记录对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="lanRecordRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="计划ID" prop="planId">
                    <el-input v-model="form.planId" placeholder="请输入计划ID"/>
                </el-form-item>
                <el-form-item label="巡检班组" prop="team">
                    <el-select v-model="form.team" clearable placeholder="请输入巡检班组">
                        <el-option v-for="item in teamList" :key="item.teamId" :value="item.teamId"
                                   :label="item.teamName"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="巡检区域" prop="region">
                    <el-input v-model="form.region" placeholder="请输入巡检区域"/>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable
                                    v-model="form.startTime"
                                    type="datetime"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    placeholder="请选择计划开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker clearable
                                    v-model="form.endTime"
                                    type="datetime"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    placeholder="请选择计划结束时间">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { listLanRecord, getLanRecord, delLanRecord, addLanRecord, updateLanRecord } from '@/api/ar_check/lanRecord'
import { getTeamListAll } from '@/api/ar_check/team.js'

defineOptions({
    name: 'LanRecord'
})

const { proxy } = getCurrentInstance()

const lanRecordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const teamList = ref([])

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: null,
        team: null,
        region: null,
        startTime: null,
        endTime: null
    },
    rules: {
        planId: [
            { required: true, message: '计划ID不能为空', trigger: 'blur' }
        ],
        team: [
            { required: true, message: '巡检班组不能为空', trigger: 'blur' }
        ],
        region: [
            { required: true, message: '巡检区域不能为空', trigger: 'blur' }
        ],
        startTime: [
            { required: true, message: '计划开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
            { required: true, message: '计划结束时间不能为空', trigger: 'blur' }
        ]
    }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询巡检计划记录列表 */
function getList() {
    loading.value = true
    listLanRecord(queryParams.value).then(response => {
        lanRecordList.value = response.rows
        total.value = response.total
        loading.value = false
    })
}

// 取消按钮
function cancel() {
    open.value = false
    reset()
}

// 表单重置
function reset() {
    form.value = {
        recordId: null,
        planId: null,
        team: null,
        region: null,
        startTime: null,
        endTime: null
    }
    proxy.resetForm('lanRecordRef')
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef')
    handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.recordId)
    single.value = selection.length !== 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset()
    getTeam()
    open.value = true
    title.value = '添加巡检计划记录'
}

async function getTeam() {
    const { data } = await getTeamListAll()
    teamList.value = data
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset()
    const _recordId = row.recordId || ids.value
    getLanRecord(_recordId).then(response => {
        form.value = response.data
        open.value = true
        title.value = '修改巡检计划记录'
    })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['lanRecordRef'].validate(valid => {
        if (valid) {
            if (form.value.recordId != null) {
                updateLanRecord(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功')
                    open.value = false
                    getList()
                })
            } else {
                addLanRecord(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功')
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _recordIds = row.recordId || ids.value
    proxy.$modal.confirm('是否确认删除巡检计划记录编号为"' + _recordIds + '"的数据项？').then(function () {
        return delLanRecord(_recordIds)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
    }).catch(() => {
    })
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('ar_check/lanRecord/export', {
        ...queryParams.value
    }, `lanRecord_${new Date().getTime()}.xlsx`)
}

getList()
</script>
